package com.lwj.mallchat.common.common.event.listener;

import com.lwj.mallchat.common.common.event.UserBlackEvent;
import com.lwj.mallchat.common.user.dao.UserDao;
import com.lwj.mallchat.common.user.domain.entity.User;
import com.lwj.mallchat.common.user.service.cache.UserCache;
import com.lwj.mallchat.common.websocket.service.WebSocketService;
import com.lwj.mallchat.common.websocket.service.adapter.WSAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author lwj
 * @ClassName UserRegisterListener
 * @date 2024/10/22 11:03
 */
@Component
public class UserBlackListener {


    @Autowired
    private UserDao userDao;

    @Autowired
    private WebSocketService webSocketService;

    @Autowired
    private UserCache userCache;

    @Async
    @EventListener(classes = UserBlackEvent.class)
    public void sendMsg(UserBlackEvent userBlackEvent) {
        //推送拉黑消息
        User user = userBlackEvent.getUser();
        webSocketService.sendMsgToAll(WSAdapter.buildBlack(user));
    }


    @Async
    @EventListener(classes = UserBlackEvent.class)
    public void changeUserStatus(UserBlackEvent userBlackEvent) {
        //改变用户状态
        User user = userBlackEvent.getUser();
        userDao.inValidByUid(user.getId());
    }

    @Async
    @EventListener(classes = UserBlackEvent.class)
    public void evictBlackMap(UserBlackEvent userBlackEvent) {
        //清空黑名单缓存
        userCache.evictBlackMap();
    }


}
